package com.kaiyidiannao.activity.myFocusOn;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import com.kaiyidiannao.activity.tour.TourLineDetailActivity;
import com.kaiyidiannao.adapter.FocusOnAdapter;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.common.pulltorefresh_view.DragListView;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.FocusOnBean;
import com.kaiyidiannao.model.req.model.PostMyFavoritesLIst;
import com.kaiyidiannao.view.ActionBarView;
import com.taplinker.core.util.LogUtil;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by wzg on 14-5-9.
 */
public class MyFocusOnActivity extends BaseActionBarActivity implements Observer{
    private DragListView listView;
    private ActionBarView actionBarView;
    private String userid;
    private String catid = "31";
    private int page = 1;
    private ArrayList<FocusOnBean> focusOnBeans ;
    private FocusOnAdapter adapter;
    private int tip = 0;
    private View no_content_view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myfoucson_main);
        DataCenter.getInstance().addObserver(this);
        init();
        if(DataCenter.userLogin.size()!=0){
            userid = DataCenter.userLogin.get(0).getUserid();
            showProgressDialog();
            new PostMyFavoritesLIst(31,"31",userid,FocusOnBean.class,page).execute();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(DataCenter.userLogin.size()!=0){
            userid = DataCenter.userLogin.get(0).getUserid();
            showProgressDialog();
            new PostMyFavoritesLIst(31,"31",userid,FocusOnBean.class,page).execute();
        }
    }

    public void init(){
        no_content_view = findViewById(R.id.no_content_view);
        actionBarView = (ActionBarView) findViewById(R.id.actionbar);
        actionBarView.title.setText("我的关注");
        actionBarView.button_right_icon2.setVisibility(View.VISIBLE);
        actionBarView.button_right_icon2.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.myfavorites_edit));
        actionBarView.button_right_icon2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                button_right_icon2_click();
            }
        });
        listView = (DragListView) findViewById(R.id.listView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MyFocusOnActivity.this, TourLineDetailActivity.class);
                intent.putExtra("catid", Integer.parseInt(focusOnBeans.get(position - 1).getCatid()));
                intent.putExtra("itemid",Integer.parseInt(focusOnBeans.get(position-1).getItemid())+"");
                startActivity(intent);
            }
        });
        listView.setOnRefreshListener(new DragListView.OnRefreshLoadingMoreListener() {
            @Override
            public void onRefresh() {
                if(DataCenter.userLogin.size()!=0){
                    userid = DataCenter.userLogin.get(0).getUserid();
                    new PostMyFavoritesLIst(catid,userid,FocusOnBean.class).execute();
                }else {
                    showToast(getText(R.string.no_network).toString());
                }
            }

            @Override
            public void onLoadMore() {
                if(DataCenter.userLogin.size()!=0){
                    userid = DataCenter.userLogin.get(0).getUserid();
                    new PostMyFavoritesLIst(catid,userid,FocusOnBean.class,page).execute();
                    page++;
                }else {
                    showToast(getText(R.string.no_network).toString());
                }
            }
        });

        adapter = new FocusOnAdapter(this,focusOnBeans);
        listView.setAdapter(adapter);
    }

    public void button_right_icon2_click(){
        if(!HttpUtils.isConnected()){
            Toast.makeText(MyFocusOnActivity.this,"网络连接错误,请稍后重试",Toast.LENGTH_SHORT).show();
            return;
        }
        if(focusOnBeans==null){
            Toast.makeText(MyFocusOnActivity.this,"暂无数据",Toast.LENGTH_SHORT).show();
            return;
        }
        if(focusOnBeans.size()==0){
            Toast.makeText(MyFocusOnActivity.this,"暂无数据",Toast.LENGTH_SHORT).show();
            return;
        }
        if(tip==0){
            DataCenter.backgroundTipBt = 1;
            actionBarView.button_right_icon2.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.favorites_complete));
            for(int i = 0 ; i<listView.getChildCount();i++){
                if(listView.getChildAt(i)!=null){
                    ImageButton btDelete = (ImageButton)listView.getChildAt(i).findViewById(R.id.btDelete);
                    if(null!=btDelete){
                        btDelete.setVisibility(View.VISIBLE);
                    }
                }
                tip = 1;
            }
        }else if(tip==1){
            Log.d("rightButton","__________");
            DataCenter.backgroundTipBt = 0;
            actionBarView.button_right_icon2.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.myfavorites_edit));
            for(int i = 1 ; i<listView.getChildCount() - 1;i++){
                if(listView.getChildAt(i)!=null){
                    final ImageButton btDelete = (ImageButton)listView.getChildAt(i).findViewById(R.id.btDelete);
                    Log.d("rightButton","__________"+btDelete);
                    if(null!=btDelete){
                        Log.d("rightButton","__________"+btDelete);
                        btDelete.setVisibility(View.GONE);
                    }
                }
                tip = 0;
            }
        }
    }


    @Override
    public void update(Observable observable, Object data) {
        DataCenter.flag flag = (DataCenter.flag) data;
        closeProgressDialog();
        if(!HttpUtils.isConnected()){
            listView.onRefreshComplete();
            listView.onLoadMoreComplete(false);
        }

        if(flag == DataCenter.flag.GET_MYFAVORITES_SUCCESS){
            focusOnBeans = DataCenter.focusOnBeans;
            if (focusOnBeans.size() == 0){
                no_content_view.setVisibility(View.VISIBLE);
                listView.setVisibility(View.INVISIBLE);
                ((TextView) no_content_view.findViewById(R.id.notice_msg_tv)).setText(getText(R.string.listview_msg_empty));
            }else{
                no_content_view.setVisibility(View.GONE);
                listView.setVisibility(View.VISIBLE);
            }
            listView.onLoadMoreComplete(false);
            listView.onRefreshComplete();
            adapter.notifyDataSetChanged();
        }
        if(flag == DataCenter.flag.POST_DELETE_MESSAGE_SUCCESS){
            focusOnBeans.remove(DataCenter.position);
        }
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
        DataCenter.resultes = "";
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogUtil.d(DataCenter.LOG_INFO, MyFocusOnActivity.class.getSimpleName() + ".onDestroy");
        DataCenter.messageInfoList.clear();
        //删除观察者
        DataCenter.getInstance().deleteObserver(this);
    }
}
